package org.jsola.hr.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsola.common.DateKit;
import org.jsola.common.DateKit8;
import org.jsola.core.Page;
import org.jsola.hr.common.ExampleKit;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.constant.MemoRemindBeforeTime;
import org.jsola.hr.dao.IMemoEmpDAO;
import org.jsola.hr.dao.IMemoInfoDAO;
import org.jsola.hr.dto.MemoInfoAddDTO;
import org.jsola.hr.dto.MemoInfoUpdateDTO;
import org.jsola.hr.dto.TaskAddDTO;
import org.jsola.hr.entity.MemoEmpDO;
import org.jsola.hr.entity.MemoInfoDO;
import org.jsola.hr.entity.TaskDO;
import org.jsola.hr.provider.INoticeProviderService;
import org.jsola.hr.query.MemoInfoQuery;
import org.jsola.hr.service.IMemoInfoService;
import org.jsola.hr.service.ITaskService;
import org.jsola.hr.service.notice.ISendNoticeService;
import org.jsola.hr.task.HrMemoRemindTask;
import org.jsola.hr.vo.*;
import org.jsola.user.core.TokenUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 备忘录
 *
 * @author zhr
 */
@Slf4j
@Service("hrMemoInfoServiceImpl")
public class MemoInfoServiceImpl implements IMemoInfoService {

    @Autowired
    private IMemoInfoDAO memoInfoDAO;

    @Autowired
    private IMemoEmpDAO memoEmpDAO;

    @Autowired
    private ITaskService taskService;

    @Autowired
    private ThreadPoolTaskScheduler threadPoolTask;

    @Autowired
    private HrMemoRemindTask hrMemoRemindTask;

    @Autowired
    private INoticeProviderService noticeProviderService;

    @Autowired
    private ISendNoticeService sendNoticeService;

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public MemoInfoVO save(MemoInfoAddDTO memoInfoAddDTO, TokenUser tokenUser) {
        // 转DO
        MemoInfoDO memoInfoDO = memoInfoAddDTO.to(MemoInfoDO.class);
        // 保存
        memoInfoDO = save(memoInfoDO, tokenUser.getSiteId(), tokenUser.getUserId());
        // 保存相关员工
        Date now = DateKit.getToday();
        if (!CollectionUtils.isEmpty(memoInfoAddDTO.getMemoEmpDOList())) {
            MemoInfoDO finalMemoInfoDO = memoInfoDO;
            memoInfoAddDTO.getMemoEmpDOList().forEach(item -> {
                item.setMemoId(finalMemoInfoDO.getId());
                item.setGmtCreate(now);
                item.setGmtModified(now);
                item.setCreateUserId(tokenUser.getUserId());
                item.setModifiedUserId(tokenUser.getUserId());
                item.setSiteId(tokenUser.getSiteId());
                item.setValid(true);
            });
            memoEmpDAO.insertList(memoInfoAddDTO.getMemoEmpDOList());
        }

        // 需要提醒
        if (memoInfoDO.getRemindNeed()) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            // 备忘录提醒时间
            Date remindDateTime = now;
            try {
                // 提醒日期 + 提醒时间
                remindDateTime = df.parse(format.format(memoInfoDO.getRemindDate()) + " " + memoInfoDO.getRemindTime());
            } catch (ParseException e) {
                log.error("提醒日期转换异常：", e);
            }
            // 发送消息通知时间（即提前提醒时间）
            Date sendNoticeDate = now;
            switch (MemoRemindBeforeTime.find(memoInfoDO.getRemindBefore())) {
                case NOW:
                    // 实时
                    sendNoticeDate = remindDateTime;
                    break;
                case BEFORE_5_MIN:
                    // 5分钟前
                    sendNoticeDate = DateKit.getBeforeMinute(remindDateTime, -5);
                    break;
                case BEFORE_15_MIN:
                    // 15分钟前
                    sendNoticeDate = DateKit.getBeforeMinute(remindDateTime, -15);
                    break;
                case BEFORE_30_MIN:
                    // 30分钟前
                    sendNoticeDate = DateKit.getBeforeMinute(remindDateTime, -30);
                    break;
                case BEFORE_1_HOUR:
                    // 1小时前
                    sendNoticeDate = DateKit.getBeforeMinute(remindDateTime, -60);
                    break;
                case BEFORE_2_HOUR:
                    // 2小时前
                    sendNoticeDate = DateKit.getBeforeMinute(remindDateTime, -60 * 2);
                    break;
                case BEFORE_1_DAY:
                    // 1天前
                    sendNoticeDate = DateKit.getBeforeDay(remindDateTime, -1);
                    break;
                case BEFORE_2_DAY:
                    // 2天前
                    sendNoticeDate = DateKit.getBeforeDay(remindDateTime, -2);
                    break;
                case BEFORE_1_WEEK:
                    // 一周前
                    sendNoticeDate = DateKit.getBeforeDay(remindDateTime, -7);
                    break;
            }
            // 当前时间小于发送消息通知时间
            if (now.before(sendNoticeDate)) {
                // 添加发送消息通知定时任务,重启时处理
                TaskAddDTO taskAddDTO = new TaskAddDTO();
                taskAddDTO.setSubjectType(HrConstants.NOTICE_TYPE_CODE_MEMO_REMIND);
                taskAddDTO.setSubjectId(memoInfoDO.getId());
                taskAddDTO.setDescription("备忘录提醒发送消息通知");
                taskAddDTO.setRunBeanName("memoInfoServiceImpl");
                taskAddDTO.setStatus(false);
                Date runDate = sendNoticeDate;
                taskAddDTO.setRunDate(runDate);
                TaskVO taskVO = taskService.save(taskAddDTO, tokenUser);
                // 添加运行定时任务
                Runnable runnable = new MemoRemindSendNoticeTask(taskVO.getId(), tokenUser);
                threadPoolTask.schedule(runnable, runDate);
            }
        }
        return memoInfoDO.to(MemoInfoVO.class);
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public List<MemoInfoVO> batchSave(List<MemoInfoAddDTO> memoInfoAddDTOList, TokenUser tokenUser) {
        if(CollectionUtils.isEmpty(memoInfoAddDTOList)) {
            return null;
        }
        List<MemoInfoDO> memoInfoDOList = memoInfoAddDTOList
                .parallelStream()
                .map(memoInfoAddDTO -> {
                    MemoInfoDO memoInfoDO = memoInfoAddDTO.to(MemoInfoDO.class);
                    memoInfoDO.preInsert(tokenUser.getUserId());
                    memoInfoDO.setSiteId(tokenUser.getSiteId());
                    return memoInfoDO;
                }).collect(Collectors.toList());
        memoInfoDAO.insertListAndSetId(memoInfoDOList);
        return  memoInfoDOList
                .parallelStream()
                .map(memoInfoDO -> memoInfoDO.to(MemoInfoVO.class))
                .collect(Collectors.toList());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int update(MemoInfoUpdateDTO memoInfoUpdateDTO, TokenUser tokenUser) {
        //转DO
        MemoInfoDO memoInfoDO = memoInfoUpdateDTO.to(MemoInfoDO.class);

        // 删除备忘录相关员工列表
        Example example = new Example(MemoEmpDO.class);
        example.and()
                .andEqualTo("memoId", memoInfoDO.getId())
                .andEqualTo("valid", true);
        memoEmpDAO.deleteByExample(example);

        // 添加备忘录相关员工列表
        if (!CollectionUtils.isEmpty(memoInfoUpdateDTO.getMemoEmpDOList())) {
            Date now = DateKit.getToday();
            memoInfoUpdateDTO.getMemoEmpDOList().forEach(item -> {
                item.setMemoId(memoInfoDO.getId());
                item.setValid(true);
                item.setGmtCreate(now);
                item.setGmtModified(now);
                item.setCreateUserId(tokenUser.getUserId());
                item.setModifiedUserId(tokenUser.getUserId());
                item.setSiteId(tokenUser.getSiteId());
            });
            memoEmpDAO.insertListAndSetId(memoInfoUpdateDTO.getMemoEmpDOList());
        }

        //根据主键更新，只更新非null值
        return updateByIdSelective(memoInfoDO, tokenUser.getSiteId(), tokenUser.getUserId());
    }

    @Override
    @Transactional(value = HrConstants.TX, rollbackFor = Exception.class)
    public int deleteByIds(TokenUser tokenUser, String... memoInfoIds) {
        return logicalDeleteByIds(tokenUser.getSiteId(), tokenUser.getUserId(), (Object[]) memoInfoIds);
    }


    @Override
    public MemoInfoVO selectById(String memoInfoId, String siteId) {
        MemoInfoDO memoInfoDO = selectDOById(memoInfoId, siteId);
        if (memoInfoDO == null) {
            return null;
        }
        Example example = new Example(MemoEmpDO.class);
        example.and()
                .andEqualTo("memoId", memoInfoId)
                .andEqualTo("siteId", siteId)
                .andEqualTo("valid", true);
        List<MemoEmpDO> memoEmpDOList = memoEmpDAO.selectByExample(example);
        MemoInfoVO memoInfoVO = memoInfoDO.to(MemoInfoVO.class);
        memoInfoVO.setMemoEmpDOList(memoEmpDOList);
        return memoInfoVO;
    }

    @Override
    public List<MemoInfoListVO> select(MemoInfoQuery memoInfoQuery, String siteId) {
        List<MemoInfoDO> memoInfoDOList = selectDO(memoInfoQuery, siteId);
        if (CollectionUtils.isEmpty(memoInfoDOList)) {
            return memoInfoDOList == null ? null : new ArrayList<>();
        }
        return memoInfoDOList.stream()
                .map(memoInfoDO -> memoInfoDO.to(MemoInfoListVO.class))
                .collect(Collectors.toList());
    }

    @Override
    public int selectCount(MemoInfoQuery memoInfoQuery, String siteId) {
        Example example = buildExample(memoInfoQuery, siteId);
        return memoInfoDAO.selectCountByExample(example);
    }

    @Override
    public Page<MemoInfoListVO> selectPage(MemoInfoQuery memoInfoQuery, String siteId) {
        Example example = buildExample(memoInfoQuery, siteId);
        Page<MemoInfoDO> page = memoInfoDAO.selectPageByExample(example,
                memoInfoQuery.getPageNo(),
                memoInfoQuery.getPageSize());

        return page.to(MemoInfoListVO.class);
    }

    @Override
    public MemoInfoDO selectDOById(String memoInfoId, String siteId) {
        return listById(memoInfoId, siteId);
    }

    @Override
    public List<MemoInfoDO> selectDO(MemoInfoQuery memoInfoQuery, String siteId) {
        Example example = buildExample(memoInfoQuery, siteId);
        return memoInfoDAO.selectByExample(example);
    }


    /**
     * 根据查询参数，构建example
     *
     * @param memoInfoQuery 查询参数
     * @param siteId        所属站点id
     * @return example
     */
    private Example buildExample(MemoInfoQuery memoInfoQuery, String siteId) {
        Example example = new Example(MemoInfoDO.class);
        example.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", siteId);
        if (memoInfoQuery.getTitle() != null) {
            example.and().andEqualTo("title", memoInfoQuery.getTitle());
        }
        if (memoInfoQuery.getContent() != null) {
            example.and().andEqualTo("content", memoInfoQuery.getContent());
        }
        if (memoInfoQuery.getRemindDateStart() != null &&
                memoInfoQuery.getRemindDateEnd() != null) {
            example.and().andBetween("remindDate", memoInfoQuery.getRemindDateStart(),
                    memoInfoQuery.getRemindDateEnd());
        }
        if (memoInfoQuery.getRemindTime() != null) {
            example.and().andEqualTo("remindTime", memoInfoQuery.getRemindTime());
        }
        if (memoInfoQuery.getRemindNeed() != null) {
            example.and().andEqualTo("remindNeed", memoInfoQuery.getRemindNeed());
        }
        if (memoInfoQuery.getRemindBefore() != null) {
            example.and().andEqualTo("remindBefore", memoInfoQuery.getRemindBefore());
        }
        if (memoInfoQuery.getCompleted() != null) {
            example.and().andEqualTo("completed", memoInfoQuery.getCompleted());
        }
        if (memoInfoQuery.getCompanyId() != null) {
            example.and().andEqualTo("companyId", memoInfoQuery.getCompanyId());
        }
        if (memoInfoQuery.getConfigId() != null) {
            example.and().andEqualTo("configId", memoInfoQuery.getConfigId());
        }
        // 排序
        ExampleKit.setExampleOrder(example, memoInfoQuery.getOrders());
        return example;
    }

    @Override
    public Integer memoRemindNotice(String subjectId, TokenUser tokenUser) {
        MemoInfoDO memoInfoDO = memoInfoDAO.selectByPrimaryKey(subjectId);
        if (Objects.nonNull(memoInfoDO)) {
            sendNoticeService.memoRemindSendNotice(memoInfoDO.getCompanyId(), memoInfoDO, tokenUser);
        }
        return null;
    }


    @Override
    public HomePageMemoRemindRelationVO getHomePageMemoRemindRelation(MemoInfoQuery memoInfoQuery, TokenUser tokenUser) {
        HomePageMemoRemindRelationVO homePageMemoRemindRelationVO = new HomePageMemoRemindRelationVO();
        Example memoInfoExample = new Example(MemoInfoDO.class);
        memoInfoExample.and()
                .andLessThanOrEqualTo("remindDate", memoInfoQuery.getRemindDate())
                .andEqualTo("createUserId", tokenUser.getUserId())
                .andEqualTo("companyId", memoInfoQuery.getCompanyId())
                .andEqualTo("siteId", tokenUser.getSiteId())
                .andEqualTo("completed", false)
                .andEqualTo("valid", true);
        memoInfoExample.orderBy("completed").asc().orderBy("gmtCreate").desc();
        // 获取今日事项相关
        List<MemoInfoDO> memoInfoDOListToday = new ArrayList<>();
        // 获取延期事项相关
        List<MemoInfoDO> memoInfoDOListDelay = new ArrayList<>();
        List<MemoInfoDO> memoInfoDOList = memoInfoDAO.selectByExample(memoInfoExample);
        for (MemoInfoDO memoInfoDO : memoInfoDOList) {
            if (memoInfoDO.getRemindTime() == null) {
                continue;
            }
            // 提醒时间
            String[] split = memoInfoDO.getRemindTime().split(":");
            if (split.length < 3) {
                continue;
            }
            // 时间
            long time = (Long.parseLong(split[0]) * 3600L + Long.parseLong(split[1]) * 60L + Long.parseLong(split[2])) * 1000L;
            // 日期
            long date = memoInfoDO.getRemindDate().getTime();
            if (date + time > System.currentTimeMillis()) {
                memoInfoDOListToday.add(memoInfoDO);
            } else {
                memoInfoDOListDelay.add(memoInfoDO);
            }
        }
        if (!CollectionUtils.isEmpty(memoInfoDOListToday)) {
//            memoInfoDOListToday = memoInfoDOListToday.stream().limit(6).collect(Collectors.toList());
            String memoIdListStr = memoInfoDOListToday.stream()
                    .map(item -> "'" + item.getId() + "'")
                    .collect(Collectors.joining(","));
            // 查询备忘录相关员工列表
            Example example = new Example(MemoEmpDO.class);
            example.and()
                    .andCondition("memo_id in (" + memoIdListStr + ")")
                    .andEqualTo("valid", true);
            List<MemoEmpDO> memoEmpDOList = memoEmpDAO.selectByExample(example);
            memoInfoDOListToday.forEach(item -> {
                List<MemoEmpDO> memoEmpDOListTmp = new ArrayList<>();
                memoEmpDOList.forEach(itemSub -> {
                    if (item.getId().equals(itemSub.getMemoId())) {
                        memoEmpDOListTmp.add(itemSub);
                    }
                });
                item.setMemoEmpDOList(memoEmpDOListTmp);
            });
        }
        Integer memoCountToday = memoInfoDOListToday.size();
        homePageMemoRemindRelationVO.setMemoCountToday(memoCountToday);
        homePageMemoRemindRelationVO.setMemoListToday(memoInfoDOListToday);
        if (!CollectionUtils.isEmpty(memoInfoDOListDelay)) {
//            memoInfoDOListDelay = memoInfoDOListDelay.stream().limit(6).collect(Collectors.toList());
            String memoIdListStr = memoInfoDOListDelay.stream()
                    .map(item -> "'" + item.getId() + "'")
                    .collect(Collectors.joining(","));
            // 查询备忘录相关员工列表
            Example example = new Example(MemoEmpDO.class);
            example.and()
                    .andCondition("memo_id in (" + memoIdListStr + ")")
                    .andEqualTo("valid", true);
            List<MemoEmpDO> memoEmpDOList = memoEmpDAO.selectByExample(example);
            memoInfoDOListDelay.forEach(item -> {
                List<MemoEmpDO> memoEmpDOListTmp = new ArrayList<>();
                memoEmpDOList.forEach(itemSub -> {
                    if (item.getId().equals(itemSub.getMemoId())) {
                        memoEmpDOListTmp.add(itemSub);
                    }
                });
                item.setMemoEmpDOList(memoEmpDOListTmp);
            });
        }
        Integer memoCountDelay = memoInfoDOListDelay.size();
        homePageMemoRemindRelationVO.setMemoCountDelay(memoCountDelay);
        homePageMemoRemindRelationVO.setMemoListDelay(memoInfoDOListDelay);
        return homePageMemoRemindRelationVO;
    }

    @Override
    public HomePageMemoRemindRelationVO getMemoRemindListToday(MemoInfoQuery memoInfoQuery, TokenUser tokenUser) {
        HomePageMemoRemindRelationVO homePageMemoRemindRelationVO = new HomePageMemoRemindRelationVO();
        // 获取今日事项相关
        Example allExample = new Example(MemoInfoDO.class);
        allExample.and()
                .andEqualTo("remindDate", memoInfoQuery.getRemindDate())
                .andEqualTo("createUserId", tokenUser.getUserId())
                .andEqualTo("companyId", memoInfoQuery.getCompanyId())
                .andEqualTo("siteId", tokenUser.getSiteId())
                .andEqualTo("valid", true);
        allExample.orderBy("completed").asc().orderBy("gmtCreate").desc();
        // 待办 + 延期
        List<MemoInfoDO> memoInfoDOList = memoInfoDAO.selectByExample(allExample);
        // 待办
        List<MemoInfoDO> memoInfoDOListToday = new ArrayList<>();
        // 延期
        List<MemoInfoDO> memoInfoDOListDelay = new ArrayList<>();
        for (MemoInfoDO memoInfoDO : memoInfoDOList) {
            if (memoInfoDO.getRemindDate() == null || StringUtils.isBlank(memoInfoDO.getRemindTime())) {
                continue;
            }
            String date = DateKit8.format2Date(memoInfoDO.getRemindDate());
            String dateTime = date + " " + memoInfoDO.getRemindTime();
            Date parseDateTime = DateKit8.parseDateTime(dateTime);
            if (parseDateTime.getTime() >= System.currentTimeMillis()) {
                if (memoInfoQuery.getRemindDateStart() != null && memoInfoQuery.getRemindDateStart().getTime() > parseDateTime.getTime()) {
                    continue;
                }
                if (memoInfoQuery.getRemindDateEnd() != null && memoInfoQuery.getRemindDateEnd().getTime() < parseDateTime.getTime()) {
                    continue;
                }
                memoInfoDOListToday.add(memoInfoDO);
            } else {
                memoInfoDOListDelay.add(memoInfoDO);
            }
        }

        Integer memoCountToday = memoInfoDOListToday.size();
        if (!CollectionUtils.isEmpty(memoInfoDOListToday)) {
            String memoIdListStr = memoInfoDOListToday.stream()
                    .map(item -> "'" + item.getId() + "'")
                    .collect(Collectors.joining(","));
            // 查询备忘录相关员工列表
            Example example = new Example(MemoEmpDO.class);
            example.and()
                    .andCondition("memo_id in (" + memoIdListStr + ")")
                    .andEqualTo("valid", true);
            List<MemoEmpDO> memoEmpDOList = memoEmpDAO.selectByExample(example);
            memoInfoDOListToday.parallelStream().forEach(item -> {
                List<MemoEmpDO> memoEmpDOListTmp = memoEmpDOList.parallelStream().map(itemSub -> {
                    if (!item.getId().equals(itemSub.getMemoId())) {
                        return null;
                    }
                    return itemSub;
                }).filter(Objects::nonNull).parallel().collect(Collectors.toList());
                item.setMemoEmpDOList(memoEmpDOListTmp);
            });
        }
        homePageMemoRemindRelationVO.setMemoCountToday(memoCountToday);
        homePageMemoRemindRelationVO.setMemoListToday(memoInfoDOListToday);

        // 获取延期事项相关

        Integer memoCountDelay = memoInfoDOListDelay.size();
        if (!CollectionUtils.isEmpty(memoInfoDOListDelay)) {
            String memoIdListDelayStr = memoInfoDOListDelay.stream()
                    .map(item -> "'" + item.getId() + "'")
                    .collect(Collectors.joining(","));
            // 查询备忘录相关员工列表
            Example exampleSub = new Example(MemoEmpDO.class);
            exampleSub.and()
                    .andCondition("memo_id in (" + memoIdListDelayStr + ")")
                    .andEqualTo("valid", true);
            List<MemoEmpDO> memoEmpDODelayList = memoEmpDAO.selectByExample(exampleSub);
            memoInfoDOListDelay.parallelStream().forEach(item -> {
                List<MemoEmpDO> memoEmpDOListTmp = memoEmpDODelayList.parallelStream().map(itemSub -> {
                    if (!item.getId().equals(itemSub.getMemoId())) {
                        return null;
                    }
                    return itemSub;
                }).filter(Objects::nonNull).parallel().collect(Collectors.toList());
                item.setMemoEmpDOList(memoEmpDOListTmp);
            });
        }
        homePageMemoRemindRelationVO.setMemoCountDelay(memoCountDelay);
        homePageMemoRemindRelationVO.setMemoListDelay(memoInfoDOListDelay);
        return homePageMemoRemindRelationVO;
    }

    @Override
    public HomePageMemoRemindRelationVO getCurrentUserNoticeList(MemoInfoQuery memoInfoQuery, String siteId) {
        return null;
    }

    @Override
    public List<MemoListVO> getCompleteMemoListByYearlyAndMonthly(Integer yearly, Integer monthly, String companyId,
                                                                  TokenUser tokenUser) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, yearly);
        calendar.set(Calendar.MONTH, monthly - 1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date calendarTime = calendar.getTime();

        Date startTime = DateKit8.getStartTime(DateKit8.getFirstDayOfMonth(calendarTime));
        Date endTime = DateKit8.getEndTime(DateKit8.getLastDayOfMonth(calendarTime));


        // 获取指定月份代办事项
        Example exampleToday = new Example(MemoInfoDO.class);
        exampleToday.and()
                .andEqualTo("valid", true)
                .andEqualTo("siteId", tokenUser.getSiteId())
                .andEqualTo("createUserId", tokenUser.getUserId())
                .andEqualTo("completed", false)
                .andGreaterThanOrEqualTo("remindDate", startTime)
                .andLessThanOrEqualTo("remindDate", endTime);
        if (StringUtils.isNotBlank(companyId)) {
            exampleToday.and().andEqualTo("companyId", companyId);
        }
        exampleToday.orderBy("completed").asc().orderBy("gmtCreate").desc();
        List<MemoInfoDO> toDoMemoList = memoInfoDAO.selectByExample(exampleToday);
        // 日期-待办事项Map
        Map<String, List<MemoInfoDO>> RemindDateStrListMap = toDoMemoList.parallelStream()
                .collect(Collectors.groupingBy(s -> DateKit8.format2Date(s.getRemindDate())));

        List<MemoListVO> memoListVOS = new ArrayList<>();
        // 当月天数
        int actualMaximum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        for (int i = 0; i < actualMaximum; i++) {

            MemoListVO memoListVO = new MemoListVO();
            memoListVO.setDay(i + 1);

            calendar.set(Calendar.DAY_OF_MONTH, i + 1);
            calendarTime = calendar.getTime();
            String date = DateKit8.format2Date(calendarTime);

            List<MemoInfoDO> memoInfoDOList = RemindDateStrListMap.get(date);
            memoListVO.setMemoList(memoInfoDOList);
            memoListVOS.add(memoListVO);
        }
        return memoListVOS;
    }

    @Override
    public Integer delByConfigIdAndDate(String configId) {
        return memoEmpDAO.delByConfigIdAndDate(configId,LocalDate.now());
    }

    public class MemoRemindSendNoticeTask implements Runnable {
        private String taskId;
        private TokenUser tokenUser;

        MemoRemindSendNoticeTask(String taskId, TokenUser tokenUser) {
            this.taskId = taskId;
            this.tokenUser = tokenUser;
        }

        @Override
        public void run() {
            TaskDO task = taskService.selectDOById(taskId, "");
            if (Objects.nonNull(task)) {
                hrMemoRemindTask.runTask(taskId, tokenUser);
            }
        }
    }
}




